'dialogwidget.bas
#include once "mud-gui/widgets/Dialog.bi"

namespace ui

        extern _MainWidget_ as BaseWidget ptr

        constructor Dialog ( byval parent as BaseWidget ptr = 0, byval width_x as integer = 0, byval height_y as integer = 0 )
        
                (BaseObject).cl = layout_t.canHaveLayout
                (BaseObject).width = width_x
                (BaseObject).height = height_y
                (BaseObject).x = 320 - (width_x / 2)
                (BaseObject).y = 240 - (height_y /2)
                m_status = DialogRet.MG_NOCLICK
        
        end constructor
        
        sub Dialog.init()
                
                (BaseObject).children->_data->width = (BaseObject).width
                (BaseObject).children->_data->height = (BaseObject).height
                (BaseObject).children->_data->x = (BaseObject).x
                (BaseObject).children->_data->y = (BaseObject).y
                
        end sub
        
        SLOT(dialogbuttonhandler_ok)
        
                cast(Dialog ptr,ref)->m_status = MG_OK
        consoleprint("ok")
                var ex = new _GUI_EVENT_ 
                ex->id = gevent.destroy
                sendEvent(ex)
                delete ex
                return 1
        
        ENDSLOT
        
        SLOT(dialogbuttonhandler_cancel)
        consoleprint("cancel")
        consoleprint("mw:  " & cast(uinteger,_MainWidget_))
        consoleprint("ref: " & cast(uinteger,ref))
                cast(Dialog ptr,ref)->m_status = MG_CANCEL
        '
                var ex = new _GUI_EVENT_ 
                ex->id = gevent.destroy
                sendEvent(ex)
                delete ex'
                return 1
        
        ENDSLOT
        
        
        sub Dialog.addButton( byval btn as BaseWidget ptr, byval btn_type as DialogRet )
        
                select case btn_type
                case MG_OK
                connectSignal(btn,clicked,dialogbuttonhandler_ok)
                case else
                connectSignal(btn,clicked,dialogbuttonhandler_cancel)
                end select
        
        end sub 
        
        function Dialog.run () as DialogRet
        
                var paintevent = new _GUI_EVENT_
                
                old_mw = _MainWidget_
                
                paintevent->id = gevent.resize
                sendEvent( toBaseWidget(@this), paintevent )
                paintevent->id = gevent.repaint
                var ret = StepMain( toBaseWidget(@this) )
                while m_status = MG_NOCLICK andalso ret >= 0
                        screenlock
                        sendEvent( toBaseWidget(@this), paintevent )
                        screenunlock
                        ret = StepMain( toBaseWidget(@this) )
                        if ret <> 0 then 
                                ConsolePrint( "dlgrun: " & ret )
                                ConsolePrint( "m_status: " & m_status )
                        endif
                        
                wend
                
                StepMain( old_mw )
                delete paintevent
                
                return ret
        
        end function 

end namespace

